package cn.edu.gzist.chapter09.config;

import cn.edu.gzist.chapter09.job.MyJob;
import cn.edu.gzist.chapter09.job.MyTaskJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail(){
        return JobBuilder.newJob(MyTaskJob.class)
                .withIdentity("job1", "group1")  //唯一标识
                .storeDurably() //持久化
                .usingJobData("count",1) //初始化
                .build();
    }
    @Bean
    public Trigger trigger(){
        // cron表达式
        String cronExpression = "0/5 * * * * ?"; //每个5秒运行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail()) // 关联JobDetail
                .withIdentity("trigger1", "group1") // 唯一标识
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
